//
//  XUISplitTableViewCell.m
//  etionUI
//
//  Created by WangJian on 14-12-18.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "XUISplitTableViewCell.h"

const float splitTableViewCellAvatarImageViewSize = 36;
const float splitTableViewCellHPadding = 10;

@interface XUISplitTableViewCell ()

//@property (nonatomic, strong) UIImageView *avatarImageView;
//@property (nonatomic, strong) UILabel *nameLabel;
//@property (nonatomic, strong) UILabel *infoLabel;

@end

@implementation XUISplitTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        if ([UIDevice isIOS7OrNewer])
        {
            self.separatorInset = UIEdgeInsetsZero;
            self.backgroundColor = [UIColor clearColor];
        }
        
        self.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease_mm];
        self.selectedBackgroundView.backgroundColor = [UIColor flatBlueColor];
    }
    return self;
}

-(void)dealloc
{
    [_nameLabel release_mm];
    [_infoLabel release_mm];
    [_avatarImageView release_mm];
    superdealloc_mm;
}

- (UIImageView *)avatarImageView
{
    if (_avatarImageView == nil)
    {
        _avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, splitTableViewCellAvatarImageViewSize, splitTableViewCellAvatarImageViewSize)];
        _avatarImageView.layer.cornerRadius = splitTableViewCellAvatarImageViewSize/2;
        _avatarImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_avatarImageView];
    }
    return _avatarImageView;
}

- (UILabel *)nameLabel
{
    if (_nameLabel == nil)
    {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        _nameLabel.font = [UIFont systemFontOfSize:14];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.numberOfLines = 2;
        _nameLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)infoLabel
{
    if (_infoLabel == nil)
    {
        _infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        _infoLabel.font = [UIFont systemFontOfSize:12];
        _infoLabel.textColor = [UIColor grayColor];
        _infoLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_infoLabel];
    }
    return _infoLabel;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    self.nameLabel.textColor = selected ? [UIColor whiteColor] : [UIColor blackColor];
    self.infoLabel.alpha = selected ? 0.0 : 1.0;
}

- (void)setAvatarImage:(UIImage *)avatarImage
{
    self.avatarImageView.image = avatarImage;
    [self setNeedsDisplay];
}

- (void)setName:(NSString *)name
{
    self.nameLabel.text = name;
    [self setNeedsDisplay];
}

- (void)setInfo:(NSString *)info
{
    self.infoLabel.text = info;
    [self setNeedsDisplay];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat startX = splitTableViewCellHPadding;
    if (self.avatarImageView.image != nil)
    {
        self.avatarImageView.center = CGPointMake(splitTableViewCellHPadding+self.avatarImageView.width/2, self.contentView.height/2);
        startX += splitTableViewCellHPadding + self.avatarImageView.width;
    }
    
    CGFloat endX = self.contentView.width-splitTableViewCellHPadding;
    if (self.infoLabel.text.length > 0)
    {
        CGSize size = [self.infoLabel.text stringSizeWithFont:self.infoLabel.font];
        self.infoLabel.frame = CGRectMake(endX-size.width, (self.contentView.height-size.height)/2, size.width, size.height);
//        if (self.status == XUISplitViewContentStatusRight || self.status == XUISplitViewContentStatusRightHide)
//        {
            endX -= size.width + splitTableViewCellHPadding;
//        }
    }
    
    self.nameLabel.frame = CGRectMake(startX, 0, endX-startX, self.contentView.height);
}

- (void)setStatus:(XUISplitViewContentStatus)status
{
//    _status = status;
//    switch (status)
//    {
//        case XUISplitViewContentStatusLeftHide:
//        case XUISplitViewContentStatusLeft:
//            self.infoLabel.alpha = 0.0;
//            break;
//        case XUISplitViewContentStatusRight:
//        case XUISplitViewContentStatusRightHide:
//            self.infoLabel.alpha = 1.0;
//            break;
//    }
//    [self setNeedsDisplay];
}

- (void)setStatus:(XUISplitViewContentStatus)status animated:(BOOL)animate
{
    
}

@end
